package com.pocketsweet.ui;

import java.util.ArrayList;
import java.util.List;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.avos.avoscloud.AVException;
import com.avos.avoscloud.AVQuery;
import com.avos.avoscloud.FindCallback;
import com.avos.avoscloud.GetCallback;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.view.annotation.ContentView;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.pocketsweet.R;
import com.pocketsweet.chat.ui.activity.BaseActivity;
import com.pocketsweet.model.MLArticle;
import com.pocketsweet.model.MLTopic;
import com.pocketsweet.ui.uilib.adapter.TopicArticleListAdapter;

@ContentView(R.layout.activity_topic_list)
public class Topic extends BaseActivity implements OnItemClickListener {

	@ViewInject(R.id.imgBack)
	private ImageView imgBack;

	@ViewInject(R.id.topicListview)
	private ListView topicListview;

	@ViewInject(R.id.message_title)
	private TextView message_title;

	private TopicArticleListAdapter topicListAdapter;
	public static List<MLArticle> articlesList = new ArrayList<MLArticle>();
	private String topicStr;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		ViewUtils.inject(this);
		Intent intent = getIntent();
		topicStr = intent.getStringExtra("topic");
		topicListAdapter = new TopicArticleListAdapter(Topic.this, articlesList);
		topicListview.setAdapter(topicListAdapter);
		getTopicList();
		message_title.setText(intent.getStringExtra("title"));
	}
	
	

	@Override
	protected void onResume() {
		super.onResume();
		topicListAdapter.notifyDataSetChanged();
	}



	private void getTopicList() {
		AVQuery<MLTopic> querys = new AVQuery<MLTopic>("MLTopic");
		querys.getInBackground(topicStr, new GetCallback<MLTopic>() {

			@Override
			public void done(MLTopic mlTopic, AVException arg1) {
				// TODO Auto-generated method stub
				if (arg1 == null) {
					AVQuery<MLArticle> query = new AVQuery<MLArticle>(
							"MLArticle");
					query.whereEqualTo("topic", mlTopic);
					query.orderByDescending("createdAt"); // 按照文章发表时间排序
					// query.include("objectId");
					query.findInBackground(new FindCallback<MLArticle>() {
						@Override
						public void done(final List<MLArticle> article,
								AVException e) {
							if (e == null) {
								if (article.size() > 0) {
									articlesList = article;
									topicListAdapter.setDatas(articlesList);
									topicListAdapter.notifyDataSetChanged();
									// topicListview.setOnItemClickListener(this);
								}
							} else {
								// 已加载完全部，取消loading
//								Log.e("====article", "nothing" + e.getMessage());

							}
						}
					});
				} else {
//					Log.e("====getTopicList", "nothing" + arg1.getMessage());

				}

			}
		});
		// topicListview = (AutoListView) findViewById(R.id.articleListview);
		// topicListAdapter = new RecommendArticleListAdapter(this,
		// advertiseList);
		// topicListview.setAdapter(topicListAdapter);
		// topicListAdapter.notifyDataSetChanged();
		// topicListview.setOnItemClickListener(this);

	}

	public void back(View v) {
		Topic.this.finish();
	}

	@Override
	public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
		// TODO Auto-generated method stub

	}

	@Override
	protected void onSaveInstanceState(Bundle outState) {
		super.onSaveInstanceState(outState);
	}

	@Override
	protected void onRestoreInstanceState(Bundle savedInstanceState) {
		super.onRestoreInstanceState(savedInstanceState);
	}
}
